package com.dongdong.zhuangji.utils;

import com.dongdong.zhuangji.entity.PileData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/5/21
 * @description 三检记录表sheet5
 */
public class ThreeInspectionsRecord {

    public static void main(String[] args) throws Exception {
        String excelFilePath = "D:\\devtool\\zhuangji\\src\\main\\resources\\template\\桩基质检及收方资料.xlsx";
        String outputFilePath = "D:\\devtool\\zhuangji\\src\\main\\resources\\template\\桩基质检及收方资料_新版.xlsx";
        Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath));

        PileData pileData = new PileData();
        pileData.setMileage(855.123);
        pileData.setPileNumber("492-1");
        pileData.setPileBottomElevation(230.132);
        pileData.setReinforcementCageLoweringTime(new Date());
        pileData.setHoleCompletionTime(new Date());
        System.out.println(new Date());
        Sheet sheet = workbook.getSheet("Sheet6");
        creatsheet6(sheet, workbook,pileData);

        sheet = workbook.getSheet("Sheet7");
        creatsheet7(sheet, workbook,pileData);
        try (FileOutputStream out = new FileOutputStream(outputFilePath)) {
            workbook.write(out);
        }
    }

    public static void creatsheet6(Sheet sheet, Workbook workbook, PileData pileData) {
        // 修改目标单元格（示例为10 B=9 1单元格）
        Row row = sheet.getRow(4);
        Cell cell = row.getCell(1);
        String originalText = cell.getStringCellValue();
        String targetText = "0000年 00月 00日"; // 要替换的文本
        String replaceText = new DateToDayTime().toLoaclDays(pileData.getReinforcementCageLoweringTime());
        // 创建基础富文本
        RichTextString richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
        String combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);

        // 创建格式字体
        Font redFont = workbook.createFont();

        redFont.setFontName("宋体");
        redFont.setFontHeight((short) 240);
        int start=0;
        int end=richText.length();

        richText.applyFont(start, end, redFont);//起点，止点，样式

        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_SINGLE);
        //redFont.setFontHeight((short) 220);

        System.out.println(combinedText);

         start=combinedText.indexOf(replaceText);
         end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式




        redFont.setColor(IndexedColors.BLACK.getIndex());
        redFont.setFontName("Wingdings");
        redFont.setUnderline(Font.U_NONE);
        //redFont.setFontHeight((short) 220);

        start=combinedText.indexOf("þ");
        end=1+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式
        cell.setCellValue(richText);


        //--------------------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
         row = sheet.getRow(6);
         cell = row.getCell(1);
         originalText = cell.getStringCellValue();

        // 创建基础富文本
         richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
         combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);





        // 创建格式字体

        redFont.setFontName("宋体");
        redFont.setFontHeight((short) 240);
        start=0;
        end=richText.length();
        richText.applyFont(start, end, redFont);//起点，止点，样式


        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_SINGLE);
        //redFont.setFontHeight((short) 220);

        System.out.println(combinedText);

         start=combinedText.indexOf(replaceText);
         end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式

        start=combinedText.lastIndexOf(replaceText);
        end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式



        redFont.setColor(IndexedColors.BLACK.getIndex());
        redFont.setFontName("Wingdings");
        redFont.setUnderline(Font.U_NONE);
        //redFont.setFontHeight((short) 220);

        start=combinedText.indexOf("þ");
        end=1+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式


        cell.setCellValue(richText);

        //---------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
        row = sheet.getRow(3);
        cell = row.getCell(7);
        cell.setCellValue("(K0+"+pileData.getMileage()+")"+pileData.getPileNumber()+"#桩"+"钢筋");


    }

    public static void creatsheet7(Sheet sheet, Workbook workbook, PileData pileData) {
        // 修改目标单元格（示例为10 B=9 1单元格）
        Row row = sheet.getRow(4);
        Cell cell = row.getCell(1);
        String originalText = cell.getStringCellValue();
        String targetText = "0000年 00月 00日"; // 要替换的文本
        String replaceText = new DateToDayTime().toLoaclDays(pileData.getHoleCompletionTime());
        // 创建基础富文本
        RichTextString richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
        String combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);

        // 创建格式字体
        Font redFont = workbook.createFont();

        redFont.setFontName("宋体");
        redFont.setFontHeight((short) 240);
        int start=0;
        int end=richText.length();

        richText.applyFont(start, end, redFont);//起点，止点，样式

        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_SINGLE);
        //redFont.setFontHeight((short) 220);

        System.out.println(combinedText);

        start=combinedText.indexOf(replaceText);
        end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式




        redFont.setColor(IndexedColors.BLACK.getIndex());
        redFont.setFontName("Wingdings");
        redFont.setUnderline(Font.U_NONE);
        //redFont.setFontHeight((short) 220);

        start=combinedText.indexOf("þ");
        end=1+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式
        cell.setCellValue(richText);


        //--------------------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
        row = sheet.getRow(6);
        cell = row.getCell(1);
        originalText = cell.getStringCellValue();

        // 创建基础富文本
        richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
        combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);





        // 创建格式字体

        redFont.setFontName("宋体");
        redFont.setFontHeight((short) 240);
        start=0;
        end=richText.length();
        richText.applyFont(start, end, redFont);//起点，止点，样式


        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_SINGLE);
        //redFont.setFontHeight((short) 220);

        System.out.println(combinedText);

        start=combinedText.indexOf(replaceText);
        end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式

        start=combinedText.lastIndexOf(replaceText);
        end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式



        redFont.setColor(IndexedColors.BLACK.getIndex());
        redFont.setFontName("Wingdings");
        redFont.setUnderline(Font.U_NONE);
        //redFont.setFontHeight((short) 220);

        start=combinedText.indexOf("þ");
        end=1+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式


        cell.setCellValue(richText);

        //---------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
        row = sheet.getRow(3);
        cell = row.getCell(7);
        cell.setCellValue("(K0+"+pileData.getMileage()+")"+pileData.getPileNumber()+"#桩"+"机械成孔");


    }

}